<?php

namespace app\index\controller;

// 首页
use app\BaseController;
use think\facade\Db;

class Index extends BaseController
{
    public function footer($BannerTypeId = 1, $nav = 0, $TypeId = 0)
    {
        //banner
        if ($BannerTypeId == 1){
            $banner = Db::name('banner')->where([
                ['status', '=', 1],
                ['type_id', '=', $BannerTypeId],
            ])->select()->toArray();
        }else{
            $banner = Db::name('banner')->where([
                ['status', '=', 1],
                ['type_id', '=', $BannerTypeId],
            ])->find();
        }
        //案例类型
        $case_type = Db::name('case_type')->where('status', '=', 1)->select()->toArray();
        //产品类型
        $product_type = Db::name('product_type')->where('status', '=', 1)->select()->toArray();
        //新闻类型
        $news_type = Db::name('news_type')->where('status', '=', 1)->select()->toArray();

        switch ($nav) {
            case 1:
                //项目案例
                foreach ($case_type as $key => &$value) {
                    $value['no'] = '';
                    if (!empty($TypeId)) {
                        if ($TypeId == $value['id']) {
                            $value['no'] = 'on';
                        }
                    } else {
                        if ($key == 0) $value['no'] = 'on';
                    }
                }
                break;
            case 2:
                //产品
                foreach ($product_type as $key => &$value) {
                    $value['no'] = '';
                    if (!empty($TypeId)) {
                        if ($TypeId == $value['id']) {
                            $value['no'] = 'on';
                        }
                    } else {
                        if ($key == 0) $value['no'] = 'on';
                    }
                }
                break;
            case 3:
                //新闻
                foreach ($news_type as $key => &$value) {
                    $value['no'] = '';
                    if (!empty($TypeId)) {
                        if ($TypeId == $value['id']) {
                            $value['no'] = 'on';
                        }
                    } else {
                        if ($key == 0) $value['no'] = 'on';
                    }
                }
                break;
        }
        //配置
        $config = freshCache('site_info');
        $this->assign('config', $config);
        $this->assign('banner', $banner);
        $this->assign('case_type', $case_type);
        $this->assign('product_type', $product_type);
        $this->assign('news_type', $news_type);
        $this->assign('nav', $BannerTypeId);
    }

    /**
     * 首页
     */
    public function index()
    {
        //公司简介图
        $company_profile_banner = Db::name('banner')->where([
            ['status', '=', 1],
            ['type_id', '=', 6],
        ])->select();
        //产品
        $product = Db::name('product')->where('status', '=', 1)->select()->toArray();
        //新闻
        $news = Db::name('news')->where('status', '=', 1)->select()->toArray();
        foreach ($news as &$value) {
            $value['up_time'] = date('F d, Y', $value['up_time']);
        }
        //项目案例
        $case = Db::name('case')->where('status', '=', 1)->select()->toArray();
        $this->footer();
        $this->assign('case', $case);
        $this->assign('news', $news);
        $this->assign('product', $product);
        $this->assign('company_profile_banner', $company_profile_banner);
        return $this->fetch('index');
    }

    /**
     * 项目案例
     */
    public function case()
    {
        $param = $this->request->param();
        $where = [];
        if (!empty($param['type_id'])) {
            $where[] = ['type_id', '=', $param['type_id']];
        }
        $where[] = ['status', '=', 1];
        //案例
        $case = Db::name('case')->where($where)->paginate(10);
        $case_data = $case->toArray();
        foreach ($case_data['data'] as &$value) {
            $value['up_time'] = date('F,d,Y', $value['up_time']);
        }
        $this->footer(2,1,$param['type_id']??0);
        $this->assign('page', $case->render());
        $this->assign('case', $case_data);
        return $this->fetch('case');
    }

    /**
     * 项目案例-详情
     */
    public function case_details()
    {
        $param = $this->request->param();
        //产品
        $case = Db::name('case')->where('id', '=', $param['id'])->find();
        $next_page = Db::name('case')->where([
            ['status', '=', 1],
            ['type_id', '=', $case['type_id']],
            ['id', '<>', $case['id']],
        ])->value('id');
        $case['up_time'] = date('Y/m/d', $case['up_time']);
        $this->footer(2,1,$param['type_id']??0);
        $this->assign('next_page', $next_page);
        $this->assign('case', $case);
        return $this->fetch('case_details');
    }

    /**
     * 关于我们
     */
    public function about()
    {
        $this->footer(2);
        return $this->fetch('index2');
    }

    /**
     * 产品中心
     */
    public function product()
    {
        $param = $this->request->param();
        $where = [];
        //banner
        $banner = Db::name('banner')->where([
            ['status', '=', 1],
            ['type_id', '=', 3],
        ])->find();
        if (!empty($param['type_id'])) {
            $where[] = ['type_id', '=', $param['type_id']];
        }
        $where[] = ['status', '=', 1];
        //产品
        $product = Db::name('product')->where($where)->paginate(10);

        $this->footer(3,2,$param['type_id']??0);
        $this->assign('page', $product->render());
        $this->assign('product', $product->toArray());
        return $this->fetch('index3');
    }

    /**
     * 产品中心-详情
     */
    public function product_details()
    {
        $param = $this->request->param();
        //产品
        $product = Db::name('product')->where('id', '=', $param['id'])->find();
        $next_page = Db::name('product')->where([
            ['status', '=', 1],
            ['type_id', '=', $product['type_id']],
            ['id', '<>', $product['id']],
        ])->value('id');
        $product_img = Db::name('product_img')->where('product_id', '=', $param['id'])->column('img');
        $this->footer(3,2,$param['type_id']??0);
        $this->assign('next_page', $next_page);
        $this->assign('product_img', $product_img);
        $this->assign('product', $product);
        return $this->fetch('product_details');
    }

    /**
     * 新闻中心
     */
    public function news()
    {
        $param = $this->request->param();
        $where = [];
        if (!empty($param['type_id'])) {
            $where[] = ['type_id', '=', $param['type_id']];
        }
        $where[] = ['status', '=', 1];
        //新闻
        $news = Db::name('news')->where($where)->paginate(10);
        $news_data = $news->toArray();
        foreach ($news_data['data'] as &$value) {
            $value['up_time'] = date('F,d,Y', $value['up_time']);
        }
        $this->footer(4,3,$param['type_id']??0);
        $this->assign('page', $news->render());
        $this->assign('news', $news_data);
        return $this->fetch('index4');
    }

    /**
     * 新闻中心-详情
     */
    public function news_details()
    {
        $param = $this->request->param();
        //产品
        $news = Db::name('news')->where('id', '=', $param['id'])->find();
        $next_page = Db::name('news')->where([
            ['status', '=', 1],
            ['type_id', '=', $news['type_id']],
            ['id', '<>', $news['id']],
        ])->value('id');
        $news['up_time'] = date('Y/m/d', $news['up_time']);
        $this->footer(4,3,$param['type_id']??0);
        $this->assign('next_page', $next_page);
        $this->assign('news', $news);
        return $this->fetch('news_details');
    }

    /**
     * 联系我们
     */
    public function contact()
    {

        $this->footer(5);
        return $this->fetch('index5');
    }

    /**
     * 检索
     */
    public function search()
    {
        $param = $this->request->param();
        $where = [];
        if (!empty($param['title'])) {
            $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        }
        $where[] = ['status', '=', 1];
        $query1 = Db::table('sp_product')
            ->field('id,1 as type,title,img,introduce')
            ->where($where);
        $query2 = Db::table('sp_news')
            ->field('id,2 as type,title,img,introduce')
            ->where($where)->buildSql();
        $query3 = Db::table('sp_case')
            ->field('id,3 as type,title,img,introduce')
            ->where($where)->buildSql();
        $sql = $query1->union([$query2,$query3], true)->buildSql(); // 设置为true以保留重复记录
        $data = Db::table($sql . ' a')->paginate(10);
        $this->footer(0);
        $this->assign('page', $data->render());
        $this->assign('data', $data->toArray());
        return $this->fetch('search');
    }

    /**
     * map
     */
    public function map()
    {
        $this->footer(0);
        return $this->fetch('map');
    }

    public function message()
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'name|名字' => 'require',
                'tel|电话' => 'require',
                'email|邮箱' => 'require',
                'message|消息' => 'require',
            ]);
            $param['time'] = time();
            Db::name('message')->insert($param);
            return success('留言成功!');
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
    }
}